﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DOTNET101_Delegate
{
    public partial class Form1 : Form
    {
        System.DateTime lastActivity;
        public Form1()
        {
            InitializeComponent();

            lastActivity = new System.DateTime();
            this.textBox1.KeyDown += this.MultiHandler; //works with KeyEventArgs
            this.button1.MouseClick += this.MultiHandler; //works with MouseEventArgs

            // Lambda expression: .NET 3.0
            this.Click += (s, e) =>
            {
                MessageBox.Show(
                    ((MouseEventArgs)e).Location.ToString());
            };

            // .NET 2.0: anonymous method
            this.Click += delegate(object o, EventArgs e)
            {
                string s = o.ToString() + " " + e.ToString();
                Console.WriteLine(s);
            };

        }

        // Event hander for any event with an EventArgs or
        // derived class in the second parameter
        private void MultiHandler(object sender, System.EventArgs e)
        {
            lastActivity = System.DateTime.Now;
            Console.WriteLine(lastActivity);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
